package leetcode;

import static java.lang.Math.abs;

public class leetcode110平衡二叉树 {
    public boolean isBalanced(TreeNode root) {
        if (root==null){
            return true;
        }else{
            int hl=height(root.left);
            int hr=height(root.right);
            if ((abs(hl-hr))<=1){
                return isBalanced(root.left)&&isBalanced(root.right);
            }else{
                return false;
            }
        }


    }
    public int height(TreeNode root){
        if (root==null){
            return 0;
        }
        int hl=height(root.left);
        int hr=height(root.right);
        if (hl>hr){
            return hl+1;
        }else{
            return hr+1;
        }
    }
}
